﻿/// <reference path="../Framework/BrowserApi.js" />
/// <reference path="../Framework/Toolbar/Toolbar.css" />
/// <reference path="Content.js" />
/// <reference path="Global.js" />
/// <reference path="SelectionProcessing.js" />
/// <reference path="Spreadsheet.js" />
/// <reference path="VirtualScrolling.js" />
/// <reference path="ToolbarItems.js" />

/*
Captures the document mouse and keyboard events, filters the one the spreadsheet needs, and calls the 
appropriate functions.
*/


var MouseAndKeyboardEvents = new function ()
{
    this.Initialize = function ()
    {        
        SpreadsheetPanel.addEventListener('mousedown', MouseAndKeyboardEvents.OnMouseDownEventHandler, false);
        SpreadsheetPanel.addEventListener('mousemove', MouseAndKeyboardEvents.OnMouseMoveEventHandler, false);
        document.body.addEventListener('mouseup', MouseAndKeyboardEvents.OnMouseUpEventHandler, false);
        document.body.addEventListener('keydown', MouseAndKeyboardEvents.OnKeyDownEventHandler, false);     
    }

    this.OnMouseDownEventHandler = function (e)
    {
        var locationX = e.pageX ;
        var locationY = e.pageY - 116;
        if (locationX <= Spreadsheet.StandardRowNumberWidth || locationY <= Spreadsheet.StandardRowHeight)
        {
            return
        }

        CellHighlight.style.visibility = 'visible'

        SelectionProcessing.SelectingCells = true;
        SelectionProcessing.SelectedArea = new Object()
        SelectionProcessing.SelectedArea.SelectedX = locationX
        SelectionProcessing.SelectedArea.SelectedY = locationY
        SelectionProcessing.SelectedArea.CurrentX = locationX
        SelectionProcessing.SelectedArea.CurrentY = locationY
        SelectionProcessing.SelectArea()
    }

    this.OnMouseMoveEventHandler = function (e)
    {
        if (!SelectionProcessing.SelectingCells)
        {
            return
        }

        var locationX = e.pageX ;
        var locationY = e.pageY - 116;
        BrowserApi.ScheduleFunction('MouseAndKeyboardEvents.OnMouseMoveEventHandler_Completed', locationX + ',' + locationY, 20)
    }

    this.OnMouseMoveEventHandler_Completed = function (pageX, pageY)
    {
        if (pageX <= Spreadsheet.StandardRowNumberWidth || pageY <= Spreadsheet.StandardRowHeight)
        {
            return
        }

        SelectionProcessing.SelectedArea.CurrentX = pageX
        SelectionProcessing.SelectedArea.CurrentY = pageY
        SelectionProcessing.SelectArea()
    }

    this.OnMouseUpEventHandler = function (e)
    {
        SelectionProcessing.SelectingCells = false;
    }

    this.OnKeyDownEventHandler = function (e)
    {
        // If the selected area is outside of the scroll area, bring the selected cell to top
        // ScrollTo(SelectedArea.SelectedX, SelectedArea.SelectedY);

        return;
        /*
        if (!this.SelectedArea.FromCell)
        {
        this.SelectedArea.FromCell = this.GetSpreadsheetCellFromLocation(100, 100);
        this.SelectedArea.ToCell = this.SelectedArea.FromCell;
        this.SelectArea()
        return;
        }


        if (event.keyCode == 37 || event.keyCode == 38 || event.keyCode == 39 || event.keyCode == 40)
        {
        columnId = this.SelectedArea.ToCell.ColumnId
        rowId = this.SelectedArea.ToCell.RowId

        if (event.keyCode == 37) // Left Arrow
        {
        if (columnId == 1)
        {
        return
        }
        selectedCell = SpreadsheetBody.rows[rowId].cells[columnId - 1];
        }
        else if (event.keyCode == 38) // Up Arrow
        {
        if (rowId == 1)
        {
        return
        }

        selectedCell = SpreadsheetBody.rows[rowId - 1].cells[columnId];
        }
        else if (event.keyCode == 39) // Right Arrow
        {s
        columnInfo = this.GetTotalRowSpanToThisCell(rowId, columnId)
        selectedCell = SpreadsheetBody.rows[rowId].cells[columnId + 1 - columnInfo.TotalColumnSpan];
        }
        else if (event.keyCode == 40) // Down Arrow
        {
        columnInfo = this.GetTotalRowSpanToThisCell(rowId + 1, columnId)
        selectedCell = SpreadsheetBody.rows[rowId + 1].cells[columnId];
        }

        CellHighlight.style.visibility = 'visible'
        this.SelectArea()
        }*/
    }
}
